﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Leestar54.WeChat.WebAPI;
using Leestar54.WeChat.WebAPI.Modal;
using Leestar54.WeChat.WebAPI.Modal.Response;
using CCWin;
using CCWin.SkinControl;

namespace Wechat_boot
{    
    public partial class Form1 : Skin_DevExpress
    {
        private class Sendc
        {
            public Sendc(string ms,int intv)
            {
                Msg = ms;
                Interval = intv;
            }
            private string msg;
            private int interval;

            public string Msg { get => msg; set => msg = value; }
            public int Interval { get => interval; set => interval = value; }
        }

        private static Client client;
        private static ChatListSubItem current_item;
        private static Dictionary<string, Sendc> sendcDict = new Dictionary<string, Sendc>();
        private static Dictionary<string, Contact> contactDict = new Dictionary<string, Contact>();
        private static Dictionary<string, Image> headimgDict = new Dictionary<string, Image>();
        private static QR_form qr_fm;
        private int total_cnt = 0;        
        public Form1()
        {
            InitializeComponent();
            client = new Client();
            qr_fm = new QR_form();
            client.GetLoginQrCodeComplete += Client_GetLoginQrCodeComplete;
            client.CheckScanComplete += Client_CheckScanComplete;
            client.LoginComplete += Client_LoginComplete;
            client.BatchGetContactComplete += Client_BatchGetContactComplete;
            client.GetContactComplete += Client_GetContactComplete;
            client.Start();
            status.Text = "获取二维码中，请扫描";
        }

        private void Client_GetContactComplete(object sender, TEventArgs<List<Contact>> e)
        {
            status.Text = ("获取联系人列表（包括公众号，联系人），总数：" + e.Result.Count);
            foreach (var item in e.Result)
            {
                if (!contactDict.Keys.Contains(item.UserName))
                {
                    contactDict.Add(item.UserName, item);
                    client.GetIconAsync(item.HeadImgUrl, item.UserName, m_set_img);
                }
                //联系人列表中包含联系人，公众号，可以通过参数做区分
                if (item.VerifyFlag != 0)
                {
                    //个人号
                }
                else
                {
                    //公众号
                }
            }
            //如果获取完成
            if (client.IsFinishGetContactList)
            {
                status.Text = ("联系人总数：" + e.Result.Count + "，正在拉取联系人头像...");
                total_cnt = e.Result.Count;
            }
        }

        private void Client_BatchGetContactComplete(object sender, TEventArgs<List<Contact>> e)
        {
            status.Text = ("拉取联系人信息，总数：" + e.Result.Count);
            foreach (var item in e.Result)
            {
                if (!contactDict.Keys.Contains(item.UserName))
                {
                    contactDict.Add(item.UserName, item);
                    client.GetIconAsync(item.HeadImgUrl,item.UserName, m_set_img);
                }
            }
        }

        private void m_set_img(M_img mimg)
        {
            if (!headimgDict.Keys.Contains(mimg.Usrname))
            {
                headimgDict.Add(mimg.Usrname, mimg.Img);
            }
            if (total_cnt > 0 && headimgDict.Count == total_cnt)
            {
                foreach (var item in headimgDict)
                {
                    CCWin.SkinControl.ChatListSubItem subitem = new ChatListSubItem();
                    subitem.HeadImage = item.Value;
                    subitem.DisplayName = contactDict[item.Key].NickName;
                    subitem.NicName = contactDict[item.Key].City;
                    subitem.PersonalMsg = contactDict[item.Key].UserName;
                    chat_box.Items[0].SubItems.Add(subitem);
                }
                status.Text = ("已完成，总好友：" + total_cnt + "，可以开始操作!");
            }
        }

        private void Client_LoginComplete(object sender, TEventArgs<User> e)
        {
            status.Text = "登录成功，正在拉取联系人信息...";
        }

        private void Client_CheckScanComplete(object sender, TEventArgs<Image> e)
        {
            qr_fm.Close();
            status.Text = "用户已扫码,正在登陆中，请稍候...";            
        }

        private void Client_GetLoginQrCodeComplete(object sender, TEventArgs<Image> e)
        {
            qr_fm.set_img(e.Result);
            qr_fm.Show();
        }

        private void chat_box_DoubleClickSubItem(object sender, CCWin.SkinControl.ChatListEventArgs e, MouseEventArgs es)
        {
            //e.SelectSubItem.
            to_friend.Text = "发送给：" + e.SelectSubItem.DisplayName;
            current_item = e.SelectSubItem;
            if (!sendcDict.Keys.Contains(e.SelectSubItem.PersonalMsg))
            {
                Sendc sendc = new Sendc(string.Empty, 0);
                sendcDict.Add(e.SelectSubItem.PersonalMsg, sendc);
            }
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void start_send_Click(object sender, EventArgs e)
        {
            if(current_item!=null)
            {
                sendcDict[current_item.PersonalMsg].Interval = Convert.ToInt32(interv_box.Value);
                sendcDict[current_item.PersonalMsg].Msg = msg_box.Text;
                send_timer.Interval = sendcDict[current_item.PersonalMsg].Interval * 1000;
            }
            
        }

        private void send_timer_Tick(object sender, EventArgs e)
        {
            foreach (var item in sendcDict)
            {
                if(item.Value.Interval>1 && !string.IsNullOrEmpty(item.Value.Msg))
                {
                    client.SendMsg(item.Value.Msg, item.Key);
                }
                
            }
        }
    }
}
